const innerAudioContext = wx.createInnerAudioContext()

Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 当为真值时表示阴性
        result: [],
        judge: false,

        curIndex: null, //音频当前下标
        media: "305_Positive_male_25_cough.wav", //音频信息对象
        currentSecond: '00:00', //页面所需当前播放的时间
        curSecond: 0, // 当前播放的秒数
        duration: null, //音频总时长
        playing: false, //是否正在播放
        currentIndex: 100
    },
    // 接口返回的数据

    Datas: [],
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const Datas = wx.getStorageInfoSync('datas');
        //console.log(this.Datas)
        if (!this.Datas.data) {
            //console.log(this.Datas)
            this.getDatas();
        } else {
            if (Date.now() - Datas.time > 1000 * 10) {
                this.getDatas();
            } else {
                this.Datas = Datas.data;
                let result = this.Datas;

                this.setData({
                    result: result
                })
            }
        }

    },
    //获取接口数据
    getDatas() {
        //console.log(this.Datas)

        wx.request({
            url: 'http://175.27.243.30:8080/api/predict/getRedictResults',
            header: {
                'content-type': 'application/json'
            },
            data: {
                openid: '1234'
            },
            method: 'post',
            success: (res) => {
                //console.log(this.Datas)
                // console.log(res.data)
                if (!res.data[0]) {
                    //console.log(this.Datas)
                    this.setData({
                        judge: true
                    })
                } else {
                    //console.log(this.Datas)
                    this.Datas = res.data
                    wx.setStorageSync('datas', {
                        time: Date.now(),
                        data: this.Datas
                    })
                    let tmp = this.Datas;
                    // console.log(tmp);
                    var result = [];

                    for (var i = 0; i < tmp.length; i++) {
                        var one = tmp[i];
                        var acc = 0;
                        var name;
                        for (var j = 0; j < one.card_result.length; j++) {
                            if (Number(one.card_result[j].acc) > acc) {
                                acc = Number(one.card_result[j].acc);
                                name = one.card_result[j].res;
                            }
                        }
                        let obj = {
                            card_result: one.card_result,
                            time: one.time,
                            possible: name,
                            acc:acc
                        }

                        result.push(obj);
                    }

                    this.setData({
                        result: result
                    })
                }

            },
            fail: (err) => {
                console.log("fail");
                this.setData({
                    judge: true
                })
            }

        });

        var that = this;



    },
    // 点击控件中间按键---播放/暂停
    playClick: function (e) {
        var index = e.currentTarget.dataset.index;
        this.setData({
            currentIndex: index
        })
        var obj = this.data.result[index];
        var audioUrl = obj.card_result[0].couch;
        // console.log(audioUrl)
        innerAudioContext.src = audioUrl;

        if (this.data.playing) {
            this.data.playing = false;
            this.setData({
                playing: this.data.playing
            })
            innerAudioContext.pause();
            return;
        } else {
            this.data.playing = true;
            this.setData({
                playing: this.data.playing
            })
            innerAudioContext.play();
            return;
        }
    },



    toResult(e) {
        var index = e.currentTarget.dataset.index;
        console.log(index);
        var item = this.data.result[index];

        wx.navigateTo({
            url: '/pages/myReport/myReport?item=' + JSON.stringify(item),
        })
    }
})